Remember?
Register
Back

Templating

By Monkeymatt last edited on Saturday, May 20, 2006 at 1:11:02 pm by Monkeymatt. Page 4.

<< Prev123456789Next >>

Here is a sample print_r() of the $code array for the template file given at the beginning of this tutorial. The comments are to help you understand what each part is:

Code

Array
( // whole array
[base] => Array
(
[****code****] => <html> // the template file with some base variables replaced (h1)
<head>
<title>Test Page for Template</title>
<style type="text/css">
table{
border: solid 2px #000000;
}
</style>
</head>
<body>
<h1>MEMEMEMEMEMEMEMEME</h1>

<{lotsopeople}>

<table>
<tr><th colspan='2'>{maker}</th></tr>

<tr><th>Name</th><th>Note</th></tr>
<{notebook}>
<tr><td>{name}</td><td>{note}</td></tr>
<{/notebook}>
</table>
<div> </div>

<{/lotsopeople}>
<{anotherloop}>
<div>We Love Loops</div>

<{/anotherloop}>
</body>
</html> // end template file
[lotsopeople] => Array // the part of the array with the information about the lotsopeople loop
(
[****code****] => // the normal, non-modified version of lotsopeople loop

<table>
<tr><th colspan='2'>{maker}</th></tr>
<tr><th>Name</th><th>Note</th></tr>
<{notebook}>
<tr><td>{name}</td><td>{note}</td></tr>

<{/notebook}>
</table>
<div> </div>


[****current_iteration****] => 1 // current version we should be replacing variables into
[0] => Array // first time through loop
(
[****code****] => // replaced information

<table>
<tr><th colspan='2'>Monkeymatt</th></tr>
<tr><th>Name</th><th>Note</th></tr>
<{notebook}>
<tr><td>{name}</td><td>{note}</td></tr>

<{/notebook}>
</table>
<div> </div>


[notebook] => Array // an embedded loop inside of the first loop, follows same pattern as above
(
[****code****] =>
<tr><td>{name}</td><td>{note}</td></tr>

[****current_iteration****] => 2
[0] => Array
(
[****code****] =>
<tr><td>Monkeymatt</td><td>Best Person in the World!</td></tr>

)

[1] => Array
(
[****code****] =>
<tr><td>Someone</td><td>Rollerblader</td></tr>


)

[2] => Array
(
[****code****] =>
<tr><td>Someone Else</td><td>Lego Guy</td></tr>

)

)

)// end first iteration of lotsopeople

[1] => Array // another time through lotsopeople
(
[****code****] =>

<table>
<tr><th colspan='2'>Classes</th></tr>
<tr><th>Name</th><th>Note</th></tr>
<{notebook}>
<tr><td>{name}</td><td>{note}</td></tr>

<{/notebook}>
</table>
<div> </div>


[notebook] => Array
(
[****code****] =>
<tr><td>{name}</td><td>{note}</td></tr>

[****current_iteration****] => 3
[0] => Array
(
[****code****] =>
<tr><td>AP European History</td><td>Hard, time consuming class</td></tr>

)

[1] => Array
(
[****code****] =>
<tr><td>Biology X</td><td>Slow, semi-interesting class</td></tr>


)

[2] => Array
(
[****code****] =>
<tr><td>Spanish 2x</td><td>Es la clase de Espanol</td></tr>

)

[3] => Array
(
[****code****] =>
<tr><td>Physics X</td><td>The BEST CLASS EVER</td></tr>

)

)

)

)

[anotherloop] => Array
(
[****code****] =>
<div>We Love Loops</div>

[****current_iteration****] => 1
[0] => Array
(
[****code****] =>

<div>We Love Loops</div>

)

)

)

)


Basically, it takes the template code from a specified section, makes a new section of the array with the name of that section from the template as the key, stores the original template code from there in '****code****' (named to prevent from messing up with normal section names). Then it keeps track of the current iteration that it is editing information in, and has a normal array with the code and sections of that iteration in it. Here is a good diagram to help you understand: (if it starts wrapping, copy and paste into a text editor and keep monospace, and no wrap)

Code

Note:*=**** ['*current_iteration*']
Items with @ after / -['*code'] ['*current_iteration']
them are ['section1@']<-[0@]--['*code'] /-['*code']
examples / \-[1] \-['section3@']--[0@]-['*code']
/ [2] ['section4'] \-[1] \['section5@']-etc.
$code['base']--['section2'] [2]
\
['*code*']





Next we can finally start replacing variables and getting sections.

<< Prev123456789Next >>